<template>
  <BasicTable @register="registerTable" :rowSelection="rowSelection" :searchInfo="searchInfo">
    <template #toolbar>
      <a-button type="primary" @click="handleCreate"> 新增</a-button>
    </template>
    <template #action="{ record }">
      <TableAction :actions="getTableAction(record)" />
    </template>
  </BasicTable>
  <CompanyUserDrawer @register="registerDrawer" @success="reload" />
</template>
<script lang="ts" setup name="companyUser">
import { reactive } from 'vue';
import { ActionItem, BasicTable, TableAction } from '@apps/antd-admin/src/components/Table';
import { useDrawer } from '@apps/antd-admin/src/components/drawer';
import CompanyUserDrawer from './components/CompanyUserDrawer.vue';
import { useBaseTable } from '@apps/antd-admin/src/hooks';
import { columns, searchFormSchema } from './data/data';
import { Api, resetPassWord } from './data/api';
import { useMessage } from '@apps/antd-admin/src/hooks/web/useMessage';

const { createMessage, createConfirm } = useMessage();
const [registerDrawer, { openDrawer }] = useDrawer();
const { prefixCls, tableContext, handleDelete, handleSuccess, handlePost, handleBatchDelete } = useBaseTable({
  designScope: 'company-user-template',
  tableProps: {
    title: '前端账号列表',
    rowKey: 'userId',
    //分页列表接口
    listUrl: Api.page,
    handlerName: 'busCompanyUserListHandler',
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    actionColumn: {
      width: 180,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
    clickToRowSelect: true,
    rowSelection: {
      type: 'radio',
    },
  },
});

const [registerTable, { reload }, { rowSelection, selectedRow, hasSelectedRows }] = tableContext;

/**
 * 行内操作栏
 */
function getTableAction(record): ActionItem[] {
  return [
    {
      label: '重置密码',
      onClick: handleResetPassword.bind(null, record),
    },
  ];
}

function handleCreate(record: Recordable) {
  record.busCompanyId = searchInfo.busCompanyId;
  openDrawer(true, {
    record,
    isUpdate: false,
  });
}

function handleEdit(record: Recordable) {
  openDrawer(true, {
    record,
    isUpdate: true,
  });
}

function handleResetPassword(record: Recordable) {
  let userId = record.userId;
  createConfirm({
    iconType: 'info',
    title: '重置密码',
    content: '是否重置密码为hezuo888888？',
    onOk: async () => {
      await resetPassWord({ userId: userId });
    },
    onCancel: async () => {},
  });
}

const searchInfo = reactive<Recordable>({});

function setSearchInfoProps(busCompanyId) {
  console.info('busCompanyId', busCompanyId);
  searchInfo.busCompanyId = busCompanyId;
}

defineExpose({
  setSearchInfoProps,
});
</script>
